/*
 * VerificaRestituicaoMobileServlet.java
 *
 * Created on 2 de Junho de 2007, 20:16
 */

package br.poo.mobile.servlets;

import br.poo.receita.model.bean.ContribuinteBean;
import br.poo.receita.model.bean.RestituicaoBean;
import br.poo.receita.model.entity.Contribuinte;
import br.poo.receita.model.entity.Restituicao;
import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Gustavo
 * @version
 */
public class VerificaRestituicaoMobileServlet extends HttpServlet {
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        String login = request.getParameter("login");
        String passwd = request.getParameter("passwd");
        
        if (request.getHeader("user-agent").startsWith("Profile/MIDP")){
            ContribuinteBean contribBean = new ContribuinteBean();
            Contribuinte contrib = contribBean.autenticaContribuinte(login,passwd);
                                    
            RestituicaoBean restituicaoBean = new RestituicaoBean();
            Restituicao restituicao = restituicaoBean.preencheRestituicao(login);
            
            DataOutputStream out = new DataOutputStream(response.getOutputStream());
            
            out.writeBoolean(contrib != null);
                                                
            if(restituicao != null && contrib != null){
                if (restituicao.getConfirmada() == 1){
                    out.writeBoolean(true);
                } else out.writeBoolean(false);
                out.writeUTF(restituicao.getDia() != null ? restituicao.getDia() : "N/A");
                out.writeFloat(restituicao.getValor());
            }
                        
            out.close();
        }
    }
    
}
